<template>
	<view class="beijingse">
		<view class="" style="padding-bottom: 70rpx;">
			<view class="dingox">
				<view class="xiaobai fonweight textcenter" style="font-size: 44rpx;">官方认证</view>
				<view class="xiaobai ershiba textcenter margin_top2">详情描述详情描述详情描述</view>
			</view>
			<view class="mainpadding" style="position: relative; top: -130rpx;">
				<view class="ffffff radius mainpadding">
					<view class="xiahuaxian">
						<view class="xiaohei ershiba fonweight">姓名</view>
						<input class="margin_top2" type="text" placeholder="请输入姓名" v-model="name">
					</view>
					<view class="xiahuaxian margin_top">
						<view class="xiaohei ershiba fonweight">收款账号</view>
						<input class="margin_top2" type="text" placeholder="请输入收款账号" v-model="get_account">
					</view>
					<view class="xiahuaxian margin_top">
						<view class="xiaohei ershiba fonweight">生活照上传（3-5张)</view>
						<view class="flexleft flex_wrap">
							<view class="flexcenter tpbox " v-for="(item,index) in shz_images_text" :key="index">
								<view class="tjtp">
									<image class="" :src="item" mode=""></image>
									<view class="deleteimg" @click="deleteimg(index,shz_images,shz_images_text)"></view>
								</view>
							</view>
							<view class="flexcenter tpbox " v-if="shz_images.length<5"
								@click="uploadImg(5,shz_images,shz_images_text)">
								<image class="tjtp" src="../static/image/system/tjtp.png" mode=""></image>
							</view>
						</view>
					</view>
					<view class="margin_top">
						<view class="xiaohei ershiba fonweight">上传身份证正发反面</view>
						<view class="flexbetween margin_top">
							<image class="sfzcs" src="../static/image/system/sfzzm.png" mode=""
								v-if="!idcard_zimage_text" @click="uploadImg(1)"></image>
							<image class="sfzcs" :src="idcard_zimage_text" mode="" v-if="idcard_zimage_text"
								@click="uploadImg(1)"></image>
							<!-- 上传后的图片样式 -->
							<image class="sfzcs" src="../static/image/system/sfzfm.png" mode=""
								v-if="!idcard_fimage_text" @click="uploadImg(2)"></image>
							<image class="sfzcs" :src="idcard_fimage_text" mode="" v-if="idcard_fimage_text"
								@click="uploadImg(2)"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="gudingdb ffffff mainpadding">
			<view class="flexleft">
				<!-- 已选中 -->
				<view class="margin_right1" v-if="type==1">
					<u-icon name="checkmark-circle-fill" color="#2979ff" size="16"></u-icon>
				</view>
				<!-- 为选中 -->
				<view class="weixz margin_right1" v-if="type==0"></view>
				<view class="strongtext xiaohui" @click="type=type?0:1">已阅览及同意
					<text class="strongtext xiaolan" @click.stop="jumpurl('/pages/system/system?type=3')">协议说明</text>
				</view>
			</view>
			<view class="bigbtn margin_top" @click="submit">提交</view>
		</view>
		<yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import ykAuthpup from "@/components/yk-authpup/yk-authpup";
	export default {
		components: {
			ykAuthpup
		},
		data() {
			return {
				name: "",
				get_account: "",
				shz_images: [],
				shz_images_text: [],
				idcard_zimage: "",
				idcard_zimage_text: "",
				idcard_fimage: "",
				idcard_fimage_text: "",
				flag: 0, //1上传身份证正面，2上传身份证反面
				type: 0,
				permissionID: '',
				sourceType: ['camera'],
				upnum: 0
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init(){
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					if(res.data.plg){
						let data = res.data.plg
						this.name = data.name
						this.get_account = data.get_account
						this.idcard_zimage = data.idcard_zimage
						this.idcard_zimage_text = data.idcard_zimage_text
						this.shz_images = data.shz_images.split(",")
						this.shz_images_text = data.shz_images_text
						this.idcard_fimage = data.idcard_fimage
						this.idcard_fimage_text = data.idcard_fimage_text
					}
				})
			},
			submit() {
				if (this.name == "") {
					httpRequest.toast("请输入姓名")
					return false
				}
				if (this.get_account == "") {
					httpRequest.toast("请输入收款账号")
					return false
				}
				if (this.shz_images.length < 3) {
					httpRequest.toast("生活照最少上传3张")
					return false
				}
				if (this.idcard_zimage == "") {
					httpRequest.toast("请上传身份证正面")
					return false
				}
				if (this.idcard_fimage == "") {
					httpRequest.toast("请上传身份证反面")
					return false
				}
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				httpRequest.request('/api/user/applyUserPlg', 'GET', {
					name:this.name,
					get_account:this.get_account,
					shz_images:this.shz_images.join(),
					idcard_zimage:this.idcard_zimage,
					idcard_fimage:this.idcard_fimage,
				}).then(res => {
					uni.hideLoading()
					if(res.code==1){
						this.tjcg()
					}else{
						httpRequest.toast(res.msg)
					}
				})
			},
			// 跳转提交成功
			tjcg() {
				uni.redirectTo({
					url: '/pages_mypage/tijiaochengg'
				})
			},
			jumpurl(url){
				uni.navigateTo({
					url
				})
			},
			deleteimg(index, upImgs, upImgs_text) {
				upImgs.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			openAuth(permissionID) {
				this.permissionID = permissionID; //这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
				setTimeout(() => {
					this.$refs['authpup'].open();
				}, 500)
			},
			uploadImg(value, upImgs, upImgs_text) {
				if (value == 1) {
					this.flag = 1
				} else if (value == 2) {
					this.flag = 2
				}else{
					this.flag = 3
				}
				let _this = this;
				let num = 1
				if (value > 2) {
					num = Number(value) - upImgs.length;
					this.upnum = num
					if (num <= 0) {
						return false;
					}
				}
				this.upnum = num
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						if (uni.getSystemInfoSync().brand.toLowerCase() === 'huawei') {
							if (res.tapIndex == 0) {
								_this.openAuth('CAMERA');
								_this.sourceType = ['camera'];
							} else {
								_this.openAuth('WRITE_EXTERNAL_STORAGE');
								_this.sourceType = ['album'];
							}
						
						} else {
							if (res.tapIndex == 0) {
						
								_this.sourceType = ['camera'];
							} else {
						
								_this.sourceType = ['album'];
							}
							_this.changeAuth();
						}
					},
					fail: function(res) {}
				});
			},
			changeAuth() {
				let _this = this;
				// 选择相机走0
				if (this.sourceType == "camera") {
					httpRequest.chooseImage(this.upnum, ['camera']).then(res => {
						for (var i = 0; i < res.length; i++) {
							_this.uploadImageService(res[i])
						}
					}).catch(err => {});
				}
				// 选择相册走1
				if (this.sourceType == "album") {
					httpRequest.chooseImage(this.upnum, ['album']).then(res => {
						for (var i = 0; i < res.length; i++) {
							_this.uploadImageService(res[i])
						}
					}).catch(err => {});
				}
			
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/addons/cos/index/upload1', url).then(res => {
					let image_item = res.fullurl;
					if (this.flag == 1) {
						this.idcard_zimage = res.url
						this.idcard_zimage_text = image_item
					} else if (this.flag == 2) {
						this.idcard_fimage = res.url
						this.idcard_fimage_text = image_item
					} else {
						this.shz_images.push(res.url)
						this.shz_images_text.push(image_item)
					}
					this.flag = 0
				}).catch(err => {});
			},

		}
	}
</script>

<style lang="scss" scoped>
	.sfzcs {
		width: 303rpx;
		height: 210rpx;
	}

	.tpbox {
		width: 33.33%;
		margin-top: 15rpx;
	}

	.tjtp {
		width: 199rpx;
		height: 199rpx;
		position: relative;

		image {
			border-radius: 10rpx;
			width: 100%;
			height: 100%;
		}
	}

	.dingox {
		padding: 40rpx 0 140rpx;
		background: linear-gradient(0deg, #FFFFFF 0%, #2361FF 100%);
	}
</style>